package com.kg.module.jtnc.nczdagl.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kg.core.annotation.AutoOperateLog;
import com.kg.core.annotation.NoRepeatSubmit;
import com.kg.core.exception.BaseException;
import com.kg.core.web.ResponseResult;
import com.kg.module.jtnc.jsydagl.entity.KrJsydagl;
import com.kg.module.jtnc.nczdagl.dto.KrNczdaglDTO;
import com.kg.module.jtnc.nczdagl.dto.convert.KrNczdaglConvert;
import com.kg.module.jtnc.nczdagl.entity.KrNczdagl;
import com.kg.module.jtnc.nczdagl.service.KrNczdaglService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 农场主档案管理表 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-02
 */
@RestController
@RequestMapping("/jtnc/nczdagl/krNczdagl")
@Api(tags = "农场主档案管理表", value = "农场主档案管理表", description = "KrNczdaglController")
public class KrNczdaglController {

    @Resource
    private KrNczdaglService krNczdaglService;
    @Resource
    private KrNczdaglConvert krNczdaglConvert;
    @ApiOperation(notes = "/jtnc/nczdagl/krNczdagl/select", value = "农场主下拉", httpMethod = "GET")
    @GetMapping("/select")
    public List<KrNczdagl> select() {
        List<KrNczdagl> list = krNczdaglService.lambdaQuery().orderByDesc(KrNczdagl::getCreateTime).list();
        return list;
    }


    @ApiOperation(notes = "/jtnc/nczdagl/krNczdagl/getById", value = "详情-农场主档案管理表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nczId", value = "ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/getById")
    @PreAuthorize("hasAuthority('jtnc:nczdagl:krNczdagl:getById')")
    public KrNczdaglDTO getById(String nczId) {
        return krNczdaglConvert.entityToDto(krNczdaglService.getById(nczId));
    }

    @ApiOperation(notes = "/jtnc/nczdagl/krNczdagl/list", value = "分页列表-农场主档案管理表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('jtnc:nczdagl:krNczdagl:list')")
    public Page<KrNczdaglDTO> list(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                               @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                               @RequestParam(value = "params", required = false) String params) {
        return krNczdaglService.pagelist(page, limit, params);
    }

    @ApiOperation(notes = "/jtnc/nczdagl/krNczdagl/add", value = "新增-农场主档案管理表", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('jtnc:nczdagl:krNczdagl:add')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/nczdagl/krNczdagl/add", logMsg = "新增-农场主档案管理表")
    public void add(@RequestBody KrNczdaglDTO krNczdaglDTO) throws BaseException {
        try {
            krNczdaglService.add(krNczdaglDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "新增失败！请重试");
        }
    }

    @ApiOperation(notes = "/jtnc/nczdagl/krNczdagl/update", value = "修改-农场主档案管理表", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('jtnc:nczdagl:krNczdagl:update')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/nczdagl/krNczdagl/update", logMsg = "修改-农场主档案管理表")
    public void update(@RequestBody KrNczdaglDTO krNczdaglDTO) throws BaseException {
        try {
            krNczdaglService.update(krNczdaglDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "修改失败！请重试");
        }
    }

    @ApiOperation(notes = "/jtnc/nczdagl/krNczdagl/delete", value = "删除-农场主档案管理表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nczIds", value = "待删除id列表", paramType = "body", required = true, allowMultiple = true, dataType = "String")
    })
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('jtnc:nczdagl:krNczdagl:delete')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/nczdagl/krNczdagl/delete", logMsg = "删除-农场主档案管理表")
    public void delete(@RequestBody String[] nczIds) throws BaseException {
        try {
            krNczdaglService.delete(Arrays.asList(nczIds));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "删除失败！请重试");
        }
    }

    @ApiOperation(notes = "/jtnc/nczdagl/krNczdagl/export/excel", value = "导出excel-农场主档案管理表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/export/excel")
    @PreAuthorize("hasAuthority('jtnc:nczdagl:krNczdagl:export:excel')")
    public String exportExcel(@RequestParam(value = "params", required = false) String params) throws BaseException {
        String result = krNczdaglService.exportExcel(params);
        if ("error".equals(result)) {
            throw new BaseException("导出Excel失败，请重试！");
        }
        return result;
    }

    @ApiOperation(notes = "/jtnc/nczdagl/krNczdagl/import/excel", value = "导入excel-农场主档案管理表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "请求", paramType = "query", required = false, dataType = "HttpServletRequest")
    })
    @PostMapping("/import/excel")
    @PreAuthorize("hasAuthority('jtnc:nczdagl:krNczdagl:import:excel')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/nczdagl/krNczdagl/import/excel", logMsg = "导入excel-农场主档案管理表")
    public ResponseResult importExcel(HttpServletRequest request) throws BaseException {
        try {
            String result = krNczdaglService.importExcel(request);
            if (StringUtils.hasText(result)) {
                // 导入失败，返回错误提示信息
                return ResponseResult.builder().code("200").message(result).build();
            } else {
                return ResponseResult.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "导入Excel失败，请重试！");
        }
    }

    @ApiOperation(notes = "/jtnc/nczdagl/krNczdagl/import/downloadTemplate", value = "下载导入模板-农场主档案管理表", httpMethod = "GET")
    @GetMapping("/import/downloadTemplate")
    public String downloadTemplate() throws BaseException {
        String result = krNczdaglService.downloadTemplate();
        if ("error".equals(result)) {
            throw new BaseException("下载导入模板失败，请重试！");
        }
        return result;
    }
}
